<?php /* УТФ-8:fix */

class SPIcontroller__manage__stdobj extends SPIcontroller__dummy
{
    private function get_var_key( $type, $topic_id, $subtype )
    {
        return "stdobj:{$type}:{$topic_id}:{$subtype}";
    }


    // ********************************************************************* //


    public function pager_reset( $topic_id )
    {
        $PAGER_VAR = $this->get_var_key( 'pager', $topic_id, 'index' );

        SPI::I('session')->set_var( $PAGER_VAR, '1' );

        return true;
    }
    
    public function pager( $topic_id, $url_base, $items_count, $items_per_page = null )
    {
        $PAGER_DEFAULT_ITEMS_PER_PAGE   = 25;
        $PAGER_VAR                      = $this->get_var_key( 'pager', $topic_id, 'index' );


        $new_page_index = (int) SPI::I('var')->get( $PAGER_VAR );

        if( $new_page_index > 0 )
        {
            SPI::I('session')->set_var( $PAGER_VAR, $new_page_index );
        }


        $current_page_index = max( 1, (int) SPI::I('session')->get_var( $PAGER_VAR ) );

        if( (int) $items_per_page == 0 )
        {
            $items_per_page = $PAGER_DEFAULT_ITEMS_PER_PAGE;
        }

        $pages_count = ceil( $items_count / $items_per_page );
        
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        for( $lp=1; $lp<=$pages_count; $lp++ )
        {
            $pages_flags[$lp] = false;
        }
        
        for( $lp=0; $lp<=1; $lp++ )
        {
            $this->pager__marker( $pages_flags, 1 + $lp );
            $this->pager__marker( $pages_flags, $pages_count - $lp );
        }

        for( $lp=0; $lp<=2; $lp++ )
        {
            $this->pager__marker( $pages_flags, $current_page_index - $lp );
            $this->pager__marker( $pages_flags, $current_page_index + $lp );
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        $obj_content = array();
        $obj_content[ 'current_page' ]    = $current_page_index;
        $obj_content[ 'items_per_page' ]  = $items_per_page;
        $obj_content[ 'firstitem_index' ] = ( $current_page_index - 1 ) * $items_per_page;
        
        $obj_content[ 'items' ] = array();
        
        for( $lp=1, $is_pasted_3dots=false; $lp<=$pages_count; $lp++ )
        {
            $page_item = false;

            if( $pages_flags[$lp] === true )
            {
                $page_item = array();
                $page_item[ 'index' ] = $lp;
                $page_item[ 'is_current' ] = $lp == $obj_content[ 'current_page' ];
                $page_item[ 'link' ] = $url_base
                                     . ( strpos( $url_base, '?' ) === false ? '?' : '&' )
                                     . urlencode( $PAGER_VAR ).'='.$lp;

                $is_pasted_3dots = false;
            }
            elseif( $is_pasted_3dots === false )
            {
                $page_item = array();
                $page_item[ '3dots' ] = true;

                $is_pasted_3dots = true;
            }
            
            if( $page_item !== false )
            {
                $obj_content[ 'items' ][] = $page_item;
            }
        }

        return $obj_content;
    }

    private function pager__marker( & $pages_flags, $mark_index )
    {
        if( isset( $pages_flags[ $mark_index ] ) === true )
        {
            $pages_flags[ $mark_index ] = true;
        }
        
        return true;
    }


    // ********************************************************************* //


    public function filter( $topic_id, $filter_id, $url_base )
    {
        $FILTER_VAR  = $this->get_var_key( "filter", $topic_id, $filter_id );


        $new_value = trim( SPI::I('var')->get( $FILTER_VAR ) );

        if( SPI::I('var')->get( $FILTER_VAR.':reset' ) == 'true' )
        {
            SPI::I('session')->set_var( $FILTER_VAR, '' );
            $this->pager_reset( $topic_id );
        }
        elseif( $new_value !== '' )
        {
            SPI::I('session')->set_var( $FILTER_VAR, $new_value );
            $this->pager_reset( $topic_id );
        }


        $obj_content = array();
        $obj_content['link_submit'] = $url_base;
        $obj_content['link_cancel'] = $url_base
                                    . ( strpos( $url_base, '?' ) === false ? '?' : '&' )
                                    . urlencode( $FILTER_VAR ).':reset=true';
        $obj_content['varname']     = $FILTER_VAR;
        $obj_content['value']       = (string) SPI::I('session')->get_var( $FILTER_VAR );

        return $obj_content;
    }


    public function filter_select( $topic_id, $filter_id, $variants, $url_base )
    {
        $FILTER_VAR  = $this->get_var_key( "filter", $topic_id, $filter_id );


        $new_value = trim( SPI::I('var')->get( $FILTER_VAR ) );

        if( SPI::I('var')->get( $FILTER_VAR.':reset' ) == 'true' )
        {
            SPI::I('session')->set_var( $FILTER_VAR, '' );
            $this->pager_reset( $topic_id );
        }
        elseif( $new_value !== '' )
        {
            SPI::I('session')->set_var( $FILTER_VAR, $new_value );
            $this->pager_reset( $topic_id );
        }


        $obj_content = array();
        $obj_content['link_submit'] = $url_base;
        $obj_content['link_cancel'] = $url_base
                                    . ( strpos( $url_base, '?' ) === false ? '?' : '&' )
                                    . urlencode( $FILTER_VAR ).':reset=true';
        $obj_content['varname']     = $FILTER_VAR;
        $obj_content['variants']    = $variants;
        $obj_content['value']       = (string) SPI::I('session')->get_var( $FILTER_VAR );

        return $obj_content;
    }


    // ********************************************************************* //


    public function order( $topic_id, $url_base, $order_fields )
    {
        $ORDER_VAR = $this->get_var_key( 'order', $topic_id, 'by' );


        $new_order = trim( SPI::I('var')->get( $ORDER_VAR ) );

        if( $new_order !== '' )
        {
            SPI::I('session')->set_var( $ORDER_VAR, $new_order );
            $this->pager_reset( $topic_id );
        }


        $cur_order_field = trim( SPI::I('session')->get_var( $ORDER_VAR ) );

        if( $cur_order_field == '' )
        {
            $cur_order_field = $order_fields[0];
            SPI::I('session')->set_var( $ORDER_VAR, $cur_order_field );
            $this->pager_reset( $topic_id );
        }


        $obj_content = array();
        $obj_content['current'] = $this->order__unpack_value( $cur_order_field );


        foreach( $order_fields as $order_field )
        {
            $one_order = $this->order__unpack_value( $order_field );

            $item_order = array();
            $item_order['is_active'] = $obj_content['current']['field'] == $one_order['field'];
            $item_order['dir'] = $obj_content['current']['dir'];

            if( $item_order['is_active'] )
            {
                $set_order_field = $one_order['field'] . ( $obj_content['current']['dir'] == '+' ? '-' : '+' );
            }
            else
            {
                $set_order_field = $one_order['field'].$one_order['dir'];
            }

            $item_order['link'] = $url_base
                                . ( strpos( $url_base, '?' ) === false ? '?' : '&' )
                                . urlencode( $ORDER_VAR ).'='.urlencode( $set_order_field );

            $obj_content[ $one_order['field'] ] = $item_order;
        }


        return $obj_content;
    }

    private function order__unpack_value( $value )
    {
        if( $value{ strlen($value)-1 } == '-' )
        {
            $order_field = rtrim( $value, '-' );
            $order_direct = '-';
        }
        else
        {
            $order_field = rtrim( $value, '+' );
            $order_direct = '+';
        }

        return array( 'field' => $order_field, 'dir' => $order_direct );
    }
}
